package com.lz.sports.utils

import android.net.Uri
import com.lz.library.extension.APP
import java.io.*


object UploadUtils {

    //临时文件夹目录
    private const val TMP_DIR = "tmp"

    //临时存在拍照
    private const val TMP_PHOTO = "$TMP_DIR/photo.jpg"

    //考生信息目录
    private const val EXAMINEE_DIR = "lz/examinee/sports"

    //导入时的路径
    private const val EXAMINEE_IMPORTS = "lz/imports/sports/data"

    //签名目录
    private const val EXAMINEE_SIGN = "${EXAMINEE_DIR}/signature"

    //考生照
    private const val EXAMINEE_IMAGE = "${EXAMINEE_DIR}/examinee"

    //考试计划路径
    private const val EXAMINEE_PLAN = "$EXAMINEE_IMPORTS/plan_info.txt"

    //考生信息路径
    private const val EXAMINEE_STUDENTS = "$EXAMINEE_IMPORTS/student/student_info.txt"

    private const val EXAMINEE_PHOTO = "$EXAMINEE_IMPORTS/student/student_images/"

    //检录时的目录
    private const val EXAMINEE_INSPECT_DIR = "${EXAMINEE_DIR}/inspect"

    //签到时的目录
    private const val EXAMINEE_SIGN_DIR = "${EXAMINEE_DIR}/sign_in"

    //确认时的目录
    private const val EXAMINEE_SURE_DIR = "${EXAMINEE_DIR}/sure"

    //临时文件目录
    private const val EXAMINEE_TMP = "${EXAMINEE_DIR}/tmp"


    //临时照片文件路径
    fun tmpPhotoPath(): File = File(APP.cacheDir, TMP_PHOTO).also { it.parentFile?.apply { if(!exists()) mkdir() } }

    //临时照片文件路径
    fun tmpPath(): File = File(APP.cacheDir, EXAMINEE_TMP).apply { if (!exists()) mkdirs() }

    //考生照目录
    fun examineeSavePath(): File = File(APP.cacheDir, EXAMINEE_IMAGE).apply { if (!exists()) mkdirs() }

    //考生签名目录
    fun signaturePath(): File = File(APP.cacheDir, EXAMINEE_SIGN).apply { if (!exists()) mkdirs() }

    //导入目录
    fun examineeImportsPath(): File = File(APP.cacheDir, EXAMINEE_IMPORTS).apply { if (!exists()) mkdirs() }

    //考生照地址
    fun examineePhotoPath(): File = File(APP.cacheDir, EXAMINEE_PHOTO)

    //检录目录
    fun examInspectPath(): File = File(APP.cacheDir, EXAMINEE_INSPECT_DIR).apply { if (!exists()) mkdirs() }

    //检录临时目录
    fun examInspectTmpPath(): File = File(APP.cacheDir, "$EXAMINEE_INSPECT_DIR/tmp").apply { if (!exists()) mkdirs() }

    //确认目录
    fun examSurePath(): File = File(APP.cacheDir, EXAMINEE_SURE_DIR).apply { if (!exists()) mkdirs() }

    //签到目录
    fun examSignInPath(): File = File(APP.cacheDir, EXAMINEE_SIGN_DIR).apply { if (!exists()) mkdirs() }

    //考试计划, 将导入的计划缓存在
    fun examineePlanPath(): File = File(APP.cacheDir, EXAMINEE_PLAN)

    fun examineeInfoPath(): File = File(APP.cacheDir, EXAMINEE_STUDENTS)

    @Throws(IOException::class)
    private fun copyFileUsingStream(source: File, dest: File) {
        var `is`: InputStream? = null
        var os: OutputStream? = null
        try {
            `is` = FileInputStream(source)
            os = FileOutputStream(dest)
            val buffer = ByteArray(1024)
            var length: Int
            while (`is`.read(buffer).also { length = it } > 0) {
                os.write(buffer, 0, length)
            }
        } finally {
            `is`!!.close()
            os!!.close()
        }
    }

    //拷贝文件
    fun copyUriFile(uri: Uri, path: String): Boolean {
        try {
            APP.contentResolver.openInputStream(uri)!!.use { inputStream ->
                FileOutputStream(path).use {
                    val buffer = ByteArray(1024)
                    var length: Int
                    while (inputStream.read(buffer).also { length = it } > 0) {
                        it.write(buffer, 0, length)
                    }
                    return true
                }
            }
        } catch (_: Exception) {
        }
        return false
    }

    fun copyFile(from: File, to: File): Boolean {
        return try {
            FileInputStream(from).use { input ->
                FileOutputStream(to).use { out ->
                    val buffer = ByteArray(1024)
                    var length: Int
                    while (input.read(buffer).also { length = it } > 0) {
                        out.write(buffer, 0, length)
                    }
                }
            }
            true
        } catch (_: Exception) {
            false
        }
    }

    fun copyFile(from: String, to: File): Boolean = copyFile(File(from), to)

    fun deleteFile(path: String) {
        File(path).also { if (it.exists()) it.delete() }
    }

    //读取文件中的字符串
    fun readTxtFile(file: File): String? {
        if (file.exists() && file.isFile) {
            val sb = StringBuilder()
            FileInputStream(file).use { fis ->
                BufferedReader(InputStreamReader(fis)).useLines { sequenceStr ->
                    sequenceStr.iterator().forEach { sb.append(it) }
                }
            }
            return sb.toString()
        }
        return null
    }
}